* read in the data
use "/Users/brucedesmarais/Dropbox/professional/Research/Submitted/Camp/Data/CandidateCluster/RepMerged.dta", clear

* DemNotTriv is an indicator of whether any Democrat in the race was not in the big catch-all cluster
* RepNotTriv '' '' Republican '' ''

* create non-trivial non-incumbent variables
gen NTrivRChal = RepNotTriv*(1-inc_rep)
gen NTrivDChal = DemNotTriv*(1-inc_dem)

* replicate Woon and Pope's model, adding in our variables
* note, because of the shorter timespan in our data, we can only really replicate their first model
* there is not enough variation on some of the ivs to estimate the other models
estimates drop _all

* TABLE 1
xtreg dhv mid pha gap presvote inc_dem inc_rep chqual_dem chqual_rep south NTrivRChal NTrivDChal dmidterm, i(year) re
estimates store t11
xtreg dhv mid pha gap pha2xgap pha1xgap pha0xgap presvote inc_dem inc_rep chqual_dem chqual_rep south NTrivRChal NTrivDChal, i(year) re
estimates store t12
estimates table t11 t12, b(%4.3f) se(%4.3f) stats(N sigma_u sigma_e r2_o r2_w r2_b)


* TABLE 2
xtreg dhv incmid incpha incpha_negxgap incpha_posxgap presvote inc_dem chqual_dem chqual_rep south dmidterm NTrivRChal NTrivDChal if incumb!="Open", re i(year)
estimates store t21
xtreg dhv incmid incpha incpha_posxgap presvote chqual_rep south dmidterm NTrivRChal NTrivDChal if incumb=="Dem", re i(year)
estimates store t22
xtreg dhv incmid incpha incpha_negxgap presvote chqual_dem south dmidterm NTrivRChal NTrivDChal if incumb=="Rep", re i(year)
estimates store t23
xtreg dhv dwlag demmean gopmean demvar gopvar presvote chqual_rep south dmidterm NTrivRChal NTrivDChal if incumb=="Dem", re i(year)
est sto t22b
test demmean gopmean demvar gopvar
xtreg dhv dwlag demmean gopmean demvar gopvar presvote chqual_dem south dmidterm NTrivRChal NTrivDChal if incumb=="Rep", re i(year)
est sto t23b
test demmean gopmean demvar gopvar
est table t21 t22 t23 t22b t23b, b(%4.3f) se(%4.3f) stats(N sigma_u sigma_e r2_o r2_w r2_b)


* TABLE 3
xtreg dhv rpmid rppha rppha_negxrpgap rppha_posxrpgap presvote inc_dem inc_rep chqual_dem chqual_rep south dmidterm NTrivRChal NTrivDChal, i(year) re
estimates store t31
xtreg dhv rpmid rppha rppha_negxrpgap rppha_posxrpgap presvote inc_dem inc_rep chqual_dem chqual_rep dmidterm NTrivRChal NTrivDChal if south==0, i(year) re
estimates store t32
xtreg dhv rpmid rppha rppha_negxrpgap rppha_posxrpgap presvote inc_dem inc_rep chqual_dem chqual_rep dmidterm NTrivRChal NTrivDChal if south==1, i(year) re
estimates store t33
estimates table t31 t32 t33, b(%4.3f) se(%4.3f) stats(N sigma_u sigma_e r2_o r2_w r2_b)


* cross tab to compare quality challengers and non-trivial cluster challengers
tab2 NTrivDChal chqual_dem if inc_rep == 1
tab2 NTrivRChal chqual_rep if inc_dem == 1






